<?php

class Emms_Form_Sponsor_Donation extends HTech_Form
{
    protected $_primary  = 'DonationId';
        
    private $_defaultSponsor;
    private $_defaultPartner;
    
    private $_rateSrc;
    private $_rateLocal;
    
    private function _init()
    {
        if(!$this->_request->isPost()) {
            try {
                $sponsors = new Sponsors();
                $this->_defaultSponsor = $sponsors->getRecord((int) $this->_request->getParam('sponsor'))->d->Sponsor;
                $this->_defaultPartner = $sponsors->getRecord((int) $this->_request->getParam('sponsor'))->d->Partner;
            } catch (Exception $e) {
                echo $e->getMessage(); exit;
            }
        } else {
            $this->_rateSrc = $this->_db->fetchOne('select Rate from exchange_rates where Date = ? and CurrencyId = ?', 
                array($this->_request->getPost('Date'),$this->_request->getPost('SrcCurrencyId')));
            $this->_rateLocal = $this->_db->fetchOne('select Rate from exchange_rates where Date = ? and CurrencyId = ?', 
                array($this->_request->getPost('Date'),$this->_conf->param->currency->local));
        }
    }
                    
    public function getValues()
    {
        $data = parent::getValues();
        
        unset($data['Sponsor']);
        unset($data['Partner']);
        
        $data['ConvCurrencyId'] = $this->_conf->param->currency->local;
        $data['ConvAmount'] = $data['SrcAmount']*($this->_rateLocal/$this->_rateSrc);
        $data['ConvTip'] = $data['SrcTip']*($this->_rateLocal/$this->_rateSrc);
        
        return $data;
    }
    
    public function __construct() 
    {
        parent::__construct(); 
        $this->_init();       
        
        $this->setName('Donation');
        
        $this->setAttrib('title', $this->translate("Donation form"));
        $this->setDescription($this->translate("Add new donation"));
        
        $donationId = new Zend_Form_Element_Hidden('DonationId'); 
        
        $sponsorId = new Zend_Form_Element_Hidden('SponsorId');
        $sponsorId->setValue((int) $this->_request->getParam('sponsor'));
        
        $sponsor = new Zend_Form_Element_Text('Sponsor');
        $sponsor->setLabel("Sponsor")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
                        
        $partner = new Zend_Form_Element_Text('Partner');
        $partner->setLabel("Partner")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $date = new Zend_Form_Element_Text('Date');
        $date->setLabel("Date")
            ->setRequired(true)
            ->addValidator('Date', true, array(
                'format' => 'yyyy-MM-dd', 
                'messages' => $this->translate("Exchange rates not available for this date")))
            ->addValidator('Callback', true, array(
                'callback' => array($this, 'checkDateCallback'),
                'messages' => $this->translate("Exchange rates not available for this date")));
                                
        $srcCurrencyId = new Zend_Form_Element_Select('SrcCurrencyId');
        $srcCurrencyId->setLabel("Currency")                
            ->setAttrib('size','4')
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addMultiOptions($this->_db->fetchPairs('select CurrencyId, Currency from currencies')); 
                       
        $srcAmount = new Zend_Form_Element_Text('SrcAmount');
        $srcAmount->setLabel("Amount")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid amount")));
        
        $srcTip = new Zend_Form_Element_Text('SrcTip');
        $srcTip->setLabel("Fee amount")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid amount")));
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type' => 'submit')));        
        
        $this->addElements(array($partner, $sponsor, $date, $srcCurrencyId, $srcAmount, $srcTip, $submit, $donationId, $sponsorId));
        
        $this->setDefaults(array('Partner' => $this->_defaultPartner, 'Sponsor' => $this->_defaultSponsor));
    }
        
    public function checkDateCallback() 
    {
        if(!$this->_rateSrc) return false;
        if(!$this->_rateLocal) return false;
        return true;    
    }

}